package com.reggie.lotterysystem.service.activitystatus.operator;

import com.reggie.lotterysystem.dao.dataobject.ActivityUserDO;
import com.reggie.lotterysystem.dao.mapper.ActivityUserMapper;
import com.reggie.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.reggie.lotterysystem.service.enumclass.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userId = convertActivityStatusDTO.getUserId();
        ActivityUserStatusEnum targetStatus = convertActivityStatusDTO.getTargetUserStatus();
        if (null == activityId
                || null == userId
                || null == targetStatus){
            return false;
        }

        List<ActivityUserDO> activityUserDOList =
                    activityUserMapper.batchSelectAUIds(activityId,userId);
        if (CollectionUtils.isEmpty(activityUserDOList)){
            return false;
        }

        for (ActivityUserDO activityUserDO : activityUserDOList){
            if (activityUserDO.getStatus()
                    .equalsIgnoreCase(targetStatus.name())){
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserId();
        ActivityUserStatusEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetUserStatus.name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
